<%-- 
    Document   : administracaoEditarUsuario
    Created on : 21/10/2010, 11:45:00
    Author     : Thiago
--%>

<%@page import="java.util.Date"%>
<%@page import="dao.ModuloPerfilDAO"%>
<%@page import="dao.PerfilDAO"%>
<%@page import="dao.UsuarioDAO"%>
<%@page import="java.net.URLEncoder"%>
<%@page import="crypt.Crypt"%>
<%@page import="java.util.ArrayList"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="bo.*"%>
<%@page import="beans.*"%>
<%@page import="autenticacao.Sessao"%>
<%

            String moduleId = "20";
            Crypt crypt = new Crypt();
            try {
                crypt.setKey(crypt.Decifragem((String) session.getAttribute("userPhrase")));
                String desafio = crypt.Cifragem(moduleId + "@" + ferramentas.DataHora.formatarDataHora(ferramentas.DataHora.retornarDataHoraAtual(), ferramentas.DataHora.DATE_TIME_FORMAT));
                if (!(new Sessao(session).validarSessao(desafio))) {
                    response.sendRedirect("/" + request.getContextPath());
                }
            } catch (Exception e) {
                response.sendRedirect("/" + request.getContextPath());
            }

%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Gestor Operacional</title>
        <link href="<%out.print(new String(request.getRequestURL()).split("pages")[0]);%>css/style.css" type="text/css" rel="stylesheet"/>
        <link href="<%out.print(new String(request.getRequestURL()).split("pages")[0]);%>css/table.css" type="text/css" rel="stylesheet"/>
        <link href="<%out.print(new String(request.getRequestURL()).split("pages")[0]);%>css/anytime.css" rel="stylesheet" type="text/css"/>
        <script type="text/javascript" src="<%out.print(new String(request.getRequestURL()).split("pages")[0]);%>scripts/jquery.js"></script>
        <script type="text/javascript" src="<%out.print(new String(request.getRequestURL()).split("pages")[0]);%>scripts/anytime.js"></script>
        <script type="text/javascript" src="<%out.print(new String(request.getRequestURL()).split("pages")[0]);%>scripts/anytimetz.js"></script>
        <script type="text/javascript" src="<%out.print(new String(request.getRequestURL()).split("pages")[0]);%>scripts/table.js"></script>
    </head>
    <body>
        <table width="100%">
            <tr>
                <td>
                    <jsp:include page = "../menu.jsp" flush = "true"/>
                </td>
            </tr>

            <tr align="center">
                <td><br/><br/>
                    <form name="form1" name="form1" method="post" action="administracaoUsuarioEditar.jsp">
                        <input type="hidden" name="action" value="executar"/>
                        <%
                                    String matricula = (String) request.getParameter("matricula");
                                    if (matricula == null) {
                                        matricula = (String) session.getAttribute("userId");
                                    } else if (matricula.isEmpty()) {
                                        matricula = (String) session.getAttribute("userId");
                                    }

                                    Usuario usr = null;
                                    try {
                                        usr = new UsuarioDAO().recuperarUsuario(matricula);
                                    } catch (Exception e) {
                                        System.out.println(e.getMessage());
                                    }

                                    String action = (String) request.getParameter("action");
                                    if ((action != null) && (action.equalsIgnoreCase("executar"))) {
                                        try {
                                            usr.setDescricao((String) request.getParameter("nome"));
                                            usr.setPerfil(new Perfil());
                                            Integer usrPerfil = null;
                                            try {
                                                String aux1[] = crypt.Decifragem((String) session.getAttribute("userPerfilId")).split("@");
                                                long time = (ferramentas.DataHora.retornarDataHoraAtual().getTime() - ferramentas.DataHora.retornarDataHora(aux1[1], ferramentas.DataHora.DATE_TIME_FORMAT).getTime());
                                                if (time < 1800000) {
                                                    usrPerfil = Integer.valueOf(aux1[0]);
                                                } else {
                                                    usrPerfil = null;
                                                }
                                            } catch (Exception e) {
                                                System.out.println(e.getMessage());
                                            }
                                            if (usrPerfil != null) {
                                                ModuloPerfil mp = new ModuloPerfil();
                                                mp.setModulo(new Modulo());
                                                mp.setPerfil(new Perfil());
                                                mp.getModulo().setId(21);
                                                mp.getPerfil().setId(usrPerfil.intValue());
                                                ArrayList<ModuloPerfil> amp = new ModuloPerfilDAO().recuperarModPerfil(mp);
                                                if ((amp != null) && (amp.size() == 1)) {
                                                   usr.getPerfil().setId(Integer.parseInt((String) request.getParameter("perfil")));
                                                }
                                            }
                                            usr.setStatus(Integer.parseInt((String) request.getParameter("status")));
                                            if ((request.getParameter("senha") != null) && (!((String) request.getParameter("senha")).isEmpty())) {
                                                usr.setSenha(crypt.Cifragem(request.getParameter("senha")));
                                            }
                                            new UsuarioDAO().atualizarUsuario(usr);
                                            out.println("<strong><center>Atualização realizada com sucesso.</center></strong><br>");
                                        } catch (Exception e) {
                                            System.out.println(e.getMessage());
                                            out.println("<strong><center>Falha na atualização: " + e.getMessage() + ".</center></strong><br>");
                                        }
                                    }
                        %>
                        <table border="1" cellspacing="0" width="300" bgcolor="#FFFFFF">
                            <tr>
                                <td width="100" colspan="2" align="center">Matricula: <% if (usr != null) {
                                                out.println(usr.getId());
                                                out.println("<input type=\"hidden\" name=\"matricula\" value=\"" + usr.getId() + "\">");
                                            }%></td>
                            </tr>
                            <tr>
                                <td width="80">Nome:</td>
                                <td align="center"><input type="text" name="nome" value="<% if (usr != null) {
                                                out.println(usr.getDescricao());
                                            }%>" ></td>
                            </tr>
                            <tr>
                                <td>Status:</td>
                                <td align="center">
                                    <select name="status" size="1">
                                        <%
                                                    if (usr != null) {
                                                        if (usr.getStatus() == Usuario.ATIVO) {
                                                            out.println("<option value=\"" + Usuario.ATIVO + "\" selected>Ativo</option>");
                                                            out.println("<option value=\"" + Usuario.INATIVO + "\">Inativo</option>");
                                                        } else {
                                                            out.println("<option value=\"" + Usuario.INATIVO + "\" selected>Inativo</option>");
                                                            out.println("<option value=\"" + Usuario.ATIVO + "\">Ativo</option>");
                                                        }
                                                    }
                                        %>
                                    </select>
                                </td>
                            </tr>
                            <tr>
                                <td>Perfil:</td>
                                <td align="center">
                                    <select name="perfil" size="1">
                                        <%
                                                    if (usr != null) {
                                                        ArrayList<Perfil> aperfil = new PerfilDAO().recuperarPerfils(null);
                                                        if ((aperfil != null) && (!aperfil.isEmpty())) {
                                                            String aux1 = "", aux2 = "";
                                                            for (int i = 0; i < aperfil.size(); i++) {
                                                                if ((usr.getPerfil() != null) && (usr.getPerfil().getId() == aperfil.get(i).getId())) {
                                                                    out.println("<option value=\"" + aperfil.get(i).getId() + "\" selected>" + aperfil.get(i).getDescricao() + "</option>");
                                                                } else {
                                                                    out.println("<option value=\"" + aperfil.get(i).getId() + "\">" + aperfil.get(i).getDescricao() + "</option>");
                                                                }
                                                            }
                                                        }
                                                    }
                                        %>
                                    </select>
                                </td>
                            </tr>
                            <tr>
                                <td>Nova Senha:</td>
                                <td align="center"><input type="password" name="senha"/></td>
                            </tr>
                            <tr>
                                <td colspan="2" align="center">
                                    <input type="submit" value="Atualizar">
                                </td>
                            </tr>
                        </table>
                    </form>
                </td>
            </tr>
        </table>
    </body>
</html>